<?php
require("view_header.php");
//1. 允许管理员进入该页面
$isAllowAccess = isset($_SESSION['administrator']);
// 2. 允许机构管理员进入该页面
$isAllowAccess |= isset($_SESSION['jgadmin']);
// 3. 允许学校的管理员创建账号
$isAllowAccess |= isset($_SESSION['xxadmin']);
if (!$isAllowAccess ){
    echo "<a href='../loginpage.php'>Please Login First!</a>";
    exit(1);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf8">
    <title> 用户导入 </title>
</head>
<body>
    <div class="container col-sm-8 panel panel-default" style="margin-left: 20px; margin-top: 20px;" id="user_import">
        <div class="col-sm-12">
            <center><h3>请按如下规则制作包含用户信息的xlsx文件</h3></center>
        </div>
        <div class="col-sm-12">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>用户名</th><th>学生姓名</th><th>年级</th><th>班级</th><th>学校(校区)</th><th>联系方式(电话、微信、QQ)</th>
                </tr>
                </thead>
                <tbody>
                    <tr><td>zs</td><td>张三</td><td>高2019级</td><td>2班</td><td>重庆市XX中学</td><td></td></tr>
                    <tr><td>lw</td><td>李五</td><td>初2019级</td><td>2班</td><td>重庆市XX中学</td><td></td></tr>
                </tbody>
            </table>
            <div class="col-sm-12" style="text-align: right">
                <a style="color: green;" href="/template/file/user_import_tpl.xlsx">xlsx文件模板下载</a>
            </div>
            <div class="col-sm-12" style="color:blue;text-align: left">
                <ul>
                    <li>初始用户名为名字拼音首字母，如：王二小（wex）。每个新用户的初始密码为：123456</li>
                    <li>当遇到重复用户名，系统会自动在用户名后添加数字避免重复，如：wer1、wer2，无需手动修改。</li>
                    <li>为避免编码问题导致导入失败，建议将xlsx文件的名称修改为英文。如：import.xlsx</li>
                </ul>
            </div>
        </div>
        <div class="col-sm-12 form-group" style="margin-top: 2rem;">
            <div class="col-sm-6">
                <input class="btn btn-default form-control" type="file" @change="tirggerFile($event)">
            </div>
            <div class="col-sm-2">
                <input class="btn btn-primary form-control" type="button" @click="upload" value="提交"/>
            </div>
            <div class="col-sm-2">
                <input class="btn btn-primary form-control" type="button" @click="returnBack" value="返回"/>
            </div>
        </div>
    </div>
    <script>
        var app = new Vue({
            el: "#user_import",
            data:{
                uploadfiles: null,
            },

            created:function () {
            },

            methods:{
                tirggerFile(event) {
                    let files = event.target.files
                    this.uploadfiles = files;
                },

                upload() {
                    let fileObj = this.uploadfiles[0]
                    let fd = new FormData()
                    fd.append('upload_file', fileObj)
                    let url = "../model/user_router.php"
                    url += "?para="+JSON.stringify({op:"import"})
                    new Promise( (resolve, reject)=>{
                        $.ajax({
                            type:'POST',
                            url:url,
                            data:fd,
                            processData:false,
                            contentType: false,
                            dataType:'json',
                            success:(retData)=> {
                                if ( retData.status ) {
                                    resolve(retData.dat);
                                }
                                else {
                                    reject(retData.msg);
                                }
                            },
                            error:()=>{
                                reject('接口无法访问')
                            }
                        })
                    }).then( (dat)=>{
                        alert('用户信息导入成功')
                        window.history.go(-1)
                    }).catch((msg)=>{
                        alert('文件上传失败: '+msg)
                    })
                },
                returnBack() {
                    window.history.go(-1)
                }
            }
        });
    </script>
</body>
</html>
<?php
//require("../oj-footer.php");
?>